package com.monicash.monicash.information;

import android.app.Activity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.monicash.monicash.information.fragment.FamilialInformationFragment;
import com.monicash.monicash.information.fragment.LaboralInformationFragment;
import com.monicash.monicash.information.fragment.LinkAccountFragment;
import com.monicash.monicash.information.fragment.PersonalInformationFragment;
import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseActivity;
import com.monicash.monicash.information.modle.KycModle;

public class CapitalInforActivity extends BaseActivity {
    public final int TAG_INDEX_ONE = 0;
    public final int TAG_INDEX_TWO = 1;
    public final int TAG_INDEX_THREE = 2;
    public final int TAG_INDEX_FOUR = 3;

    private final String FRAGMENTNAMEONE = "fragmentOne";
    private final String FRAGMENTNAMETWO = "fragmentTwo";
    private final String FRAGMENTNAMETHREE = "fragmentThree";
    private final String FRAGMENTNAMEFOUR = "fragmentFour";
    private FragmentManager fragmentManager;

    private PersonalInformationFragment personalInformation;
    private LaboralInformationFragment laboralInformation;
    private FamilialInformationFragment familialInformation;
    private LinkAccountFragment linkAccount;

    FrameLayout mFlSafety;
    ImageView mIvSafetyClose;
    TextView mTvBancarias;
    TextView mTvFamiliares;
    TextView mTvLaborales;
    TextView mmTvPersonales;
    FrameLayout mFlBaseView;

    public KycModle kycModle = null;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capital_infor);
        //解决崩溃Fragment重叠问题
        if (savedInstanceState != null && fragmentManager != null) {
            if (fragmentManager.findFragmentByTag(FRAGMENTNAMEONE) != null) {
                personalInformation = (PersonalInformationFragment) fragmentManager.findFragmentByTag(FRAGMENTNAMEONE);
            }
            if (fragmentManager.findFragmentByTag(FRAGMENTNAMETWO) != null) {
                laboralInformation = (LaboralInformationFragment) fragmentManager.findFragmentByTag(FRAGMENTNAMETWO);
            }
            if (fragmentManager.findFragmentByTag(FRAGMENTNAMETHREE) != null) {
                familialInformation = (FamilialInformationFragment) fragmentManager.findFragmentByTag(FRAGMENTNAMETHREE);
            }
            if (fragmentManager.findFragmentByTag(FRAGMENTNAMEFOUR) != null) {
                linkAccount = (LinkAccountFragment) fragmentManager.findFragmentByTag(FRAGMENTNAMEFOUR);
            }
        }
        kycModle = KycModle.Companion.getInstant();
        initView();
        initData();

    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        fragmentManager = getSupportFragmentManager();
        mFlSafety = findViewById(R.id.fl_safety);
        mIvSafetyClose = findViewById(R.id.iv_safety_close);
        mTvBancarias = findViewById(R.id.tv_bancarias);
        mTvFamiliares = findViewById(R.id.tv_familiares);
        mTvLaborales = findViewById(R.id.tv_laborales);
        mmTvPersonales = findViewById(R.id.tv_personales);
        mFlBaseView = findViewById(R.id.fl_base_view);
        onTabSelected(TAG_INDEX_ONE);

    }

    @Override
    public void initData() {

    }

    public void onTabSelected(int index) {
//        clearSelection();//清除选中的状态
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case TAG_INDEX_ONE: {
                mmTvPersonales.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_complete));
                mmTvPersonales.setTextColor(getActivity().getColor(R.color.black));
                if (personalInformation == null) {
                    personalInformation = new PersonalInformationFragment();
                    transaction.add(R.id.fl_base_view, personalInformation, FRAGMENTNAMEONE);
                } else {
                    transaction.show(personalInformation);
                }
            }
            break;

            case TAG_INDEX_TWO: {
                mTvLaborales.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_complete));
                mTvLaborales.setTextColor(getActivity().getColor(R.color.black));
                if (laboralInformation == null) {
                    laboralInformation = new LaboralInformationFragment();
                    transaction.add(R.id.fl_base_view, laboralInformation, FRAGMENTNAMETWO);
                } else {
                    transaction.show(laboralInformation);
                }
            }
            break;

            case TAG_INDEX_THREE: {
                mTvFamiliares.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_complete));
                mTvFamiliares.setTextColor(getActivity().getColor(R.color.black));
                if (familialInformation == null) {
                    familialInformation = new FamilialInformationFragment();
                    transaction.add(R.id.fl_base_view, familialInformation, FRAGMENTNAMETHREE);
                } else {
                    transaction.show(familialInformation);
                }
            }
            break;

            case TAG_INDEX_FOUR: {
                mTvBancarias.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_complete));
                mTvBancarias.setTextColor(getActivity().getColor(R.color.black));
                if (linkAccount == null) {
                    linkAccount = new LinkAccountFragment();
                    transaction.add(R.id.fl_base_view, linkAccount, FRAGMENTNAMEFOUR);
                } else {
                    transaction.show(linkAccount);
                }
            }
            break;
        }
        transaction.commit();
    }

    private void clearSelection() {
        //清除被选中的颜色
        mTvBancarias.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_not_completed));
        mTvBancarias.setTextColor(this.getResources().getColor(R.color.grey_999999));

        mTvFamiliares.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_not_completed));
        mTvFamiliares.setTextColor(this.getResources().getColor(R.color.grey_999999));

        mTvLaborales.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_not_completed));
        mTvLaborales.setTextColor(this.getResources().getColor(R.color.grey_999999));

        mmTvPersonales.setBackground(getActivity().getDrawable(R.drawable.icon_schedule_not_completed));
        mmTvPersonales.setTextColor(this.getResources().getColor(R.color.grey_999999));

    }

    private void hideFragments(FragmentTransaction transaction) {
        if (personalInformation != null) {
            transaction.hide(personalInformation);
        }
        if (laboralInformation != null) {
            transaction.hide(laboralInformation);
        }

        if (familialInformation != null) {
            transaction.hide(familialInformation);
        }

        if (linkAccount != null) {
            transaction.hide(linkAccount);
        }
    }
}
